package com.example.agriapp.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.example.agriapp.R;
import com.example.agriapp.data.AgriDatabaseHelper;
import com.example.agriapp.model.bean.Product;
import com.example.agriapp.util.SharedPrefUtils;

import java.io.Serializable;

public class ProductDetailsActivity extends AppCompatActivity {

    private TextView visitStoreBtn, addCartBtn, buyNowBtn;
    private ImageView shoucang;
    private AgriDatabaseHelper agriDatabaseHelper;
    private int userId;

    @SuppressLint({"MissingInflatedId", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_product_details);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.bottom_nav_icon_selected));


        Product product = (Product) getIntent().getSerializableExtra("product");
        agriDatabaseHelper = new AgriDatabaseHelper(this);
        userId = SharedPrefUtils.getUserId(this);
        ImageView productImage = findViewById(R.id.productImage);

        //浏览记录
        boolean hasBrowsedProduct = agriDatabaseHelper.hasBrowsedProduct(userId, product.getId());
        if (!hasBrowsedProduct) {
            agriDatabaseHelper.addBrowsingHistory(userId, product.getId());
        }
        ImageView ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(view -> finish());
        shoucang = findViewById(R.id.shoucang);
        boolean favorite = agriDatabaseHelper.isFavorite(userId, product.getId());
        shoucang.setImageResource(favorite ? R.drawable.ic_love_yes : R.drawable.ic_love_no);
        shoucang.setOnClickListener(view -> {
            if (favorite) {
                agriDatabaseHelper.removeFavorite(userId, product.getId());
                shoucang.setImageResource(R.drawable.ic_love_no);
            } else {
                agriDatabaseHelper.addFavorite(userId, product.getId());
                shoucang.setImageResource(R.drawable.ic_love_yes);
            }
        });
        TextView productPrice = findViewById(R.id.productPrice);
        TextView productName = findViewById(R.id.productName);
        TextView minQuantity = findViewById(R.id.minQuantity);
        visitStoreBtn = findViewById(R.id.visitStoreBtn);
        LinearLayout dianPu = findViewById(R.id.dianPu);
        addCartBtn = findViewById(R.id.addCartBtn);
        buyNowBtn = findViewById(R.id.buyNowBtn);

        Glide.with(this)
                .load(product.getImage())
                .into(productImage);
        productPrice.setText(String.valueOf(product.getPrice()) + "");
        productName.setText(product.getTitle());
        minQuantity.setText(product.getCategory());
        visitStoreBtn.setOnClickListener(view -> {
            startActivity(new Intent(ProductDetailsActivity.this, MainActivity.class));
            finish();
        });
        dianPu.setOnClickListener(view -> {
            startActivity(new Intent(ProductDetailsActivity.this, MainActivity.class));
            finish();
        });

        addCartBtn.setOnClickListener(view -> {
            boolean b = agriDatabaseHelper.addToCart(userId, product.getId(), 1);
            Toast.makeText(this, b ? "添加成功" : "添加失败", Toast.LENGTH_SHORT).show();
        });

        buyNowBtn.setOnClickListener(view -> {
            boolean b = agriDatabaseHelper.createOrder(userId, product.getId(), 1);
            if (b) {
                Toast.makeText(this,  "购买成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "购买失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}